package com.gupao.jly521.simple;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jly
 * @date 2018/8/22  14:36
 */
public class SimplePrototype implements Cloneable ,Serializable{

    private String name;

    private int age;

    private List<String> list ;

    @Override
    protected Object clone() {
//        return clone1();
        return clone2();
    }

    /**
     *  方法二
     * @date 2018/8/22 15:15
     * @author Jly
     * @param []
     * @return java.lang.Object
     */
    private Object clone2() {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            bis = new ByteArrayInputStream(bos.toByteArray());//////切记
            ois = new ObjectInputStream(bis);

            SimplePrototype simpleObj = (SimplePrototype)ois.readObject();
            return simpleObj;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     *  方法一
     * @date 2018/8/22 15:15
     * @author Jly
     * @param []
     * @return java.lang.Object
     */
    private Object clone1() {
        SimplePrototype tmp = null; //拷贝仅仅是浅拷贝
        try {
            tmp = (SimplePrototype) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        List<String> listOld = tmp.getList();
        tmp.setList(new ArrayList<String>());
        tmp.getList().addAll(listOld);
        return tmp;
    }

    public static void main(String[] args) {
        SimplePrototype simple = new SimplePrototype();
        simple.setAge(12);
        simple.setName("Jly");
        simple.setList(new ArrayList<String>());
        simple.getList().add("aa");
        simple.getList().add("bb");
        simple.getList().add("cc");
        try {

            SimplePrototype simpleNew = (SimplePrototype)simple.clone();
            System.out.println(simple==simpleNew);
            System.out.println(simple.getName()+","+simple.getAge()+","+simple.getList());
            System.out.println(simpleNew.getName()+","+simpleNew.getAge()+","+simpleNew.getList());
            simple.getList().add("dd");
            simpleNew.getList().add("dddd");
            simpleNew.setName("jly521");
            System.out.println(simple.getName()+","+simple.getAge()+","+simple.getList());
            System.out.println(simpleNew.getName()+","+simpleNew.getAge()+","+simpleNew.getList());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }
}
